home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / DNet / dnettcp-stub.c < prev    next >
Text File  |  1996-07-05  |  9KB  |  435 lines

  1. /* dnettcp.c
  2.      d.g.gilbert
  3.      used as interface b/n "dirty" c and c++
  4.      for some of the tcp sockets basic stuff
  5. */
  6.  
  7. #include "dnettcp.h"
  8. #include <ncbi.h>
  9.  
  10. #define TCPSTUBS 1
  11.  
  12. #ifdef TCPSTUBS
  13.  
  14. typedef struct sockaddr *SockAddPtr; /* codewar 4 hates (struct *) casts !? */
  15.  
  16. #define INT short
  17.  
  18.     enum {
  19.         AF_INET,SOCK_STREAM, SOL_SOCKET, SO_LINGER, SO_REUSEADDR, SO_KEEPALIVE
  20.         };
  21.  
  22.     struct in_addr {
  23.         unsigned long s_addr;
  24.     };
  25.     
  26.     struct sockaddr_in {
  27.         unsigned char    sin_len;
  28.         unsigned char    sin_family;
  29.         unsigned short    sin_port;
  30.         struct    in_addr sin_addr;
  31.         char    sin_zero[8];
  32.     };
  33.     
  34.     struct    hostent {
  35.         char    *h_name;    /* official name of host */
  36.         char    **h_aliases;    /* alias list */
  37.         long    h_addrtype;    /* host address type */
  38.         long    h_length;    /* length of address */
  39.         char    **h_addr_list;    /* list of addresses from name server */
  40.     #define    h_addr    h_addr_list[0]    /* address, for backward compatiblity */
  41.     };
  42.     
  43.     
  44.     unsigned long inet_addr( char* hostname)
  45.     {
  46.         return 0;
  47.     }
  48.  
  49.     short connect(short sockfd, SockAddPtr Server, size_t serversize)
  50.     {
  51.         return 0;
  52.     }
  53.  
  54.     short socket( short AF_INET, short SOCK_STREAM, short val)
  55.     {
  56.         return 0;
  57.     }
  58.  
  59.     short s_close(long theSocket)
  60.     {
  61.         return 0;
  62.     }
  63.  
  64.     short s_write(long itsSocket, void* buffer, long buflen) 
  65.     {
  66.         return 0;
  67.     }
  68.  
  69.     void setsockopt(short sockfd, short SOL_SOCKET,  short SO_LINGER, short x, short y)
  70.     {
  71.  
  72.     }
  73.  
  74.     long gethostid()
  75.     {
  76.         return 1;
  77.     }
  78.  
  79.     short gethostname( char* name, short namelen)
  80.     {
  81.         return 1;
  82.     }
  83.  
  84.     short htons(short port)
  85.     {
  86.         return port;
  87.     }
  88.  
  89.     typedef struct hostent *hostPtr1;
  90.  
  91.     hostPtr1 gethostbyname( char* hostname)
  92.     {
  93.         return (hostPtr1) 0;
  94.     }
  95.  
  96.     short getsockname( short itsSocket, SockAddPtr name, long* len)
  97.     {
  98.         if (len) *len= 0;
  99.         return 0;
  100.     }
  101.  
  102.     short s_read(short itsSocket, void* buffer, size_t buflen)
  103.     {
  104.         return 1;
  105.     }
  106.  
  107.     typedef long fd_set;
  108.     struct timeval {
  109.         unsigned long s_addr;
  110.         };
  111.     typedef struct timeval *timeptr;
  112.  
  113.     short select( short numsocks, fd_set* readsocks, 
  114.                   fd_set* writesocks, fd_set* errsocks, timeptr time)
  115.   {
  116.         return 0;
  117.     }
  118.  
  119.  
  120.  
  121.  
  122. #else
  123.  
  124. /********* all of real headers... */
  125.  
  126. #ifdef COMP_CODEWAR
  127. #define __STDC__
  128.  
  129. /* weird, this proto isn't in any of those many tcp headers !! */
  130. int gethostname(char * machname,long buflen);
  131.  
  132. #endif
  133.  
  134. typedef struct sockaddr *SockAddPtr; /* codewar 4 hates (struct *) casts !? */
  135.  
  136. #define __NI_LIB__
  137. #define _NCBINET_
  138. #define _NI_TYPES_
  139.  
  140. #ifdef WIN_MSWIN
  141. #define NETP_INET_WSOCK 1
  142.     /* those who want non-winsock will have to fiddle here... */
  143. #endif
  144.  
  145. #ifdef OS_MAC
  146.  
  147. /* #include "macsockd.h" << this defines s_socketstuff as socketstuff, conflicting standard c lib */
  148. /* just do a subset.... */
  149. #define socket                s_socket
  150. #define connect                s_connect
  151. #define getsockname        s_getsockname
  152. #define setsockopt        s_setsockopt
  153.  
  154. #define __TYPES__       /* avoid Mac <Types.h> */
  155. #define __MEMORY__      /* avoid Mac <Memory.h> */
  156. #define ipBadLapErr     /* avoid Mac <MacTCPCommonTypes.h> */
  157. #define APPL_SOCK_DEF
  158. #define DONT_DEFINE_INET
  159. #include "sock_ext.h"
  160. extern void bzero PROTO((CharPtr target, long numbytes));
  161. #endif /* OS_MAC */
  162.  
  163.  
  164.     /* this is from NCBI network/nsclilib/ -- it defines & includes system-specific headers */
  165. #include "ni_net.h"
  166.  
  167.  
  168. #endif  /* STUBS */
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176. int InitSocks PROTO((void));
  177.  
  178. static int gSocks_on = 0;
  179.  
  180. int InitSocks()
  181. {
  182. #ifdef WIN_MSWIN
  183.     if (!gSocks_on) {
  184.         int err;
  185.         WSADATA data;
  186.         WORD version = (1 << 8) + 1; /* MAKEWORD(1,1); */
  187.         err= WSAStartup(version, &data);
  188.         gSocks_on= (err == 0);
  189.         }
  190.     return gSocks_on;
  191. #else
  192.     return 1;
  193. #endif
  194. }
  195.  
  196.  
  197.  
  198. long SockOpen(char* hostname, unsigned short port)
  199. {
  200.             /* from Paul Lindner, UMinnesota gopher Socket.c  */
  201.      struct sockaddr_in Server;
  202.      struct hostent*    hostPtr;
  203.      long sockfd = 0;
  204.      long ERRinet = -1;
  205. #ifdef _CRAY
  206.      ERRinet = 0xFFFFFFFF;  /* -1 doesn't sign extend on 64 bit machines */
  207. #endif
  208.      long errnum = 0;
  209.  
  210.      /*** Find the hostname address ***/
  211.     if (!InitSocks()) return -1;
  212.      
  213. #ifdef WIN_MSWIN
  214.     if (WSAIsBlocking()) WSACancelBlockingCall(); /* ??? */
  215. #endif
  216.  
  217.     MemFill( &Server, '\0', sizeof(Server));  /* dgg, mswin fix? */
  218.     Server.sin_addr.s_addr = inet_addr(hostname);
  219.     if (Server.sin_addr.s_addr == ERRinet) {
  220.         hostPtr = gethostbyname(hostname);
  221.         if (hostPtr != NULL) {
  222.             MemFill( &Server, '\0', sizeof(Server));
  223.             MemCopy( &Server.sin_addr, hostPtr->h_addr, hostPtr->h_length);
  224.             Server.sin_family = hostPtr->h_addrtype;
  225.             }
  226.         else {
  227.             errnum= errHost;
  228.             goto errexit; /* return (errHost); */
  229.             }
  230.     } 
  231.     else
  232.       Server.sin_family = AF_INET;
  233.  
  234.     Server.sin_port = (unsigned short) htons(port);
  235.  
  236.      /*** Open the socket ***/
  237.  
  238.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  239.         errnum= errSocket;
  240.         goto errexit; /* return (errSocket);  */
  241.         }
  242.  
  243. #ifndef WIN_MSWIN
  244. /* ?? winsock is having some problems */
  245. #ifndef UCX
  246.     setsockopt(sockfd, SOL_SOCKET, ~SO_LINGER, 0, 0);
  247. #endif
  248.     setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, 0, 0);
  249.     setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, 0, 0);
  250. #endif
  251.     
  252.         /*** Connect ***/
  253.  
  254.     if (connect(sockfd, (SockAddPtr) &Server, sizeof(Server)) < 0) {
  255.         SockClose(sockfd);
  256.         errnum= errConnect;
  257.         goto errexit; /* return (errnum); */
  258.         }
  259.     
  260.     return(sockfd);
  261.  
  262. errexit:
  263. #ifdef WIN_MSWIN
  264.     WSACancelBlockingCall(); /* ??? */
  265. #endif
  266.     return(errnum);
  267. /* #else -- !NETLIB && !CMUIP  << left out these versions, dgg */
  268. }
  269.  
  270.  
  271. short SockClose(long theSocket)
  272. {
  273. #ifdef WIN_MSWIN
  274.     return closesocket(theSocket);
  275. #else
  276. #ifdef WIN_MAC
  277.     return s_close(theSocket);
  278. #else
  279.     return close(theSocket);
  280. #endif
  281. #endif
  282. }
  283.  
  284.  
  285.     
  286. long    MyIPaddress(void)
  287. {
  288.     if (!InitSocks()) return 0;
  289. #ifdef WIN_MSWIN
  290.     /* winsok seems to lack gethostid() */
  291.     return inet_addr("localhost");
  292. #else
  293.     return gethostid();
  294. #endif
  295. }
  296.  
  297.  
  298. short    MyHostname( char* name, short namelen)
  299. {
  300.     if (!InitSocks()) return -1;
  301.     return (short) gethostname( name, namelen);
  302. }
  303.  
  304. long    Hostname2IP( char* hostname)
  305. {
  306.     if (!InitSocks()) return -1;
  307.     return (long) gethostbyname( (char*)hostname);
  308. }
  309.  
  310.  
  311. short    SockHostname( long itsSocket, char* name, short namelen)
  312. {
  313.     long    len= namelen;
  314.     short err;
  315.     err= getsockname( itsSocket, (SockAddPtr) name, &len); 
  316.     if (!err) { if (len>=namelen) --len; name[len]= '\0'; }
  317.     return err;
  318. }
  319.  
  320. long    SockRead( long itsSocket, void *buffer, long buflen)
  321. {
  322. #ifdef WIN_MSWIN
  323.     return recv(itsSocket, buffer, buflen, 0);
  324. #else
  325. #ifdef WIN_MAC
  326.     return s_read(itsSocket, buffer, buflen);
  327. #else
  328.     return read(itsSocket, buffer, buflen);
  329. #endif
  330. #endif
  331. }
  332.  
  333. long    SockWrite( long itsSocket, void *buffer, long buflen)
  334. {
  335. #ifdef WIN_MSWIN
  336.     return send(itsSocket, buffer, buflen, 0);
  337. #else
  338. #ifdef WIN_MAC
  339.     return s_write(itsSocket, buffer, buflen);
  340. #else
  341.     return write(itsSocket, buffer, buflen);
  342. #endif
  343. #endif
  344. }
  345.  
  346. short    SockSelect( short numsocks, long readsocks[],long writesocks[],
  347.                                 long errsocks[], long time)
  348. {
  349.     return select( numsocks, (fd_set*)readsocks, (fd_set*)writesocks, (fd_set*)errsocks, (struct timeval *) time);
  350. }
  351.  
  352.  
  353. #if defined(NOT_COMP_CODEWAR) && defined(OS_MAC)
  354. /* damn that buggy linker, can't *find* this code in netdb.c */
  355.  
  356. //# include <Stdio.h>
  357. //# include <Types.h>
  358. //# include <Resources.h>
  359. //# include <Errors.h>
  360. //# include <OSUtils.h>
  361.  
  362. # include <s_types.h>
  363. //# include <netdb.h>
  364. //# include <neti_in.h>
  365. //# include <s_socket.h>
  366. # include <s_time.h>
  367. # include <neterrno.h>
  368.  
  369. //# include "sock_str.h"
  370. /*# include "sock_int.h"*/
  371.  
  372. //#include <Ctype.h>
  373. #include <a_namesr.h>
  374. #include <s_param.h>
  375.  
  376.  
  377.  
  378. extern SpinFn spinroutine;
  379. extern pascal void DNRDone(struct hostInfo *hostinfoPtr,Boolean *done);
  380.  
  381. extern int h_errno;
  382.  
  383. struct hostent * gethostbyname(char *name)
  384. {
  385.     Boolean done;
  386.     int i;
  387.     
  388.     sock_init();
  389.  
  390.     for (i=0; i<NUM_ALT_ADDRS; i++)
  391.         macHost.addr[i] = 0;
  392.     done = false;
  393.     if (StrToAddr(name,&macHost,(ResultProcPtr) DNRDone,(char *) &done) == cacheFault)
  394.     {
  395.         SPINP(!done,SP_NAME,0L)
  396.     }
  397.     switch (macHost.rtnCode)
  398.     {
  399.         case noErr: break;
  400.         
  401.         case nameSyntaxErr:    h_errno = HOST_NOT_FOUND;    return(NULL);
  402.         case cacheFault:    h_errno = NO_RECOVERY;        return(NULL);
  403.         case noResultProc:    h_errno = NO_RECOVERY;        return(NULL);
  404.         case noNameServer:    h_errno = HOST_NOT_FOUND;    return(NULL);
  405.         case authNameErr:    h_errno = HOST_NOT_FOUND;    return(NULL);
  406.         case noAnsErr:        h_errno = TRY_AGAIN;        return(NULL);
  407.         case dnrErr:        h_errno = NO_RECOVERY;        return(NULL);
  408.         case outOfMemory:    h_errno = TRY_AGAIN;        return(NULL);
  409.         case notOpenErr:    h_errno = NO_RECOVERY;        return(NULL);
  410.         default:            h_errno = NO_RECOVERY;        return(NULL);
  411.     }
  412.     
  413.     /* was the 'name' an IP address? */
  414.     if (macHost.cname[0] == 0)
  415.     {
  416.         h_errno = HOST_NOT_FOUND;
  417.         return(NULL);
  418.     }
  419.     
  420.     /* for some reason there is a dot at the end of the name */
  421.     i = strlen(macHost.cname) - 1;
  422.     if (macHost.cname[i] == '.')
  423.         macHost.cname[i] = 0;
  424.     
  425.     for (i=0; i<NUM_ALT_ADDRS && macHost.addr[i]!=0; i++)
  426.     {
  427.         addrPtrs[i] = &macHost.addr[i];
  428.     }
  429.     addrPtrs[i] = NULL;
  430.     
  431.     return(&unixHost);
  432. }
  433.  
  434. #endif
  435.